/**
 * Created by sun on 2017/3/28.
 */
define(function (require) {
    var app = require('/../js/app');
    require('ui-table');
    var toastr =require('toastr');
    app.useModule("ui.table");
    var datepicker = require('datepicker');
    app.controller('creditCtrl', ['$scope','$http','$filter' ,function ($scope, $http,$filter) {
        $scope.ctrlScope=$scope;
        $scope.ctrlScope.itemsearch={};
        $scope.isEdit=false;
        $scope.add={};
        $scope.orderParams = {
            url: "/admin/assetDeposit/getPageList"
        };
        //全局查询
        $scope.search = function() {
            //表格查询参数设置
            $scope.orderParams.ajaxParam=$scope.ctrlScope.itemsearch;
            //表格刷新
            $scope.orderParams.reload();
        };
        //全局查询重置
        $scope.reset=function(){
            $scope.orderParams.ajaxParam={};
            $scope.ctrlScope.itemsearch={};
            $scope.orderParams.reload();
        };
        //新增
        $scope.addModal=function(){
            $scope.add={};
            $scope.isEdit=false;
            $scope.detailModal=!$scope.detailModal;
        };
        //修改
        $scope.modifyBtn=function(id){
            $scope.detailModal=!$scope.detailModal;
            $scope.getOne(id);
            $scope.isEdit=true;
        };
        //查看单个
        $scope.getOne=function(id){
            $http({
                method: 'POST',
                url: "/admin/assetDeposit/findById",
                data:{id:id}
            }).success(function(data) {
                $scope.add=data;
            }).error(function(data) {
            });
        };
        //提交与修改
        $scope.addBtn=function(){
            var param=$scope.add;
            $http({
                method: 'POST',
                url: "/admin/assetDeposit/save",
                data:param
            }).success(function(data) {
                if(data.result=='success'){
                    if( $scope.isEdit==true){
                        toastr.success('修改成功');
                    }else{
                        toastr.success('新增成功');
                    }
                    $scope.detailModal=!$scope.detailModal;
                    $scope.orderParams.reload();
                }else{
                    if( $scope.isEdit==true){
                        toastr.error('修改失败');
                    }else{
                        toastr.error('新增失败');
                    }
                }
            }).error(function(data) {
            });
        };
        //获取公司下拉框
        $scope.getCompany=function(){
            $http({
                method: 'POST',
                url: "/admin/assetCompany/getList"
            }).success(function(data) {
                $scope.companyList=data;
            }).error(function(data) {
            });
        };
        $scope.getCompany();

        //删除
        $scope.removeBtn=function(id){
            if(confirm("确认删除此条商品?")) {
                $http({
                    method: 'POST',
                    url: "/admin/assetDeposit/delete",
                    data: {
                        id:id
                    }
                }).success(function(data) {
                    if(data.result=="success"){
                        toastr.success("删除成功");
                        $scope.orderParams.reload();
                    }else{
                        toastr.error("删除失败");
                        return;
                    }
                }).error(function(data) {});
            } else {
                return;
            }
        };

    }]);
});